// pages/profileTab/userInfo/userInfo.js
const app = getApp()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        //加载自全局数据，全局数据在应用启动授权时获得
        userInfo: {},
        role: ["学员", "导师"],
        roleIndex: 0,
        plantform: ["微信", "QQ"],
        plantformIndex: 0,
        userid: "000001",
        gameName:'',
        phone: '请完善手机号',
        userType:'',
        platform1:null,
        roleIndex:'',
        role1:'',
        // 修改游戏昵称(简称cgm)的弹窗的状态
        showCgnModalStatus: false,

        hiddenmodalput: true,
        //是否修改成功
        changeOk: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
      var that = this;
      var phone = app.globalData.phone;
      var setphone;
      if (phone!=null) {
        setphone = app.globalData.phone;
      }else{
        setphone = '请完善手机号'
      }
      if (app.globalData.userInfo) { //如果有全局用户数据则加载
          this.setData({
              userInfo: app.globalData.userInfo,
              gameName: app.globalData.nickName,
              phone:setphone,
              userType: app.globalData.userType,
              platform1: app.globalData.platform,
              // role1: app.globalData.userType
          })
          console.log("页面数据加载完成！", app.globalData.userInfo)
      } else {
          console.log("没有全局用户数据！")
      }
      wx.request({
        url: 'https://www.enjoyyz.com:8443/yinzuo/getUserInf',
        data: {
          openid: app.globalData.openid
        },
        success(res) {
          console.log(res.data.gameNickname,'res.data.gameNickname')
          that.setData({
            userid: res.data.userId,
            plantformIndex: res.data.gamePlatform,
            gameName: res.data.gameNickname,
           
          })
        }
      })
    },


//手机号
  modalinput: function () {
    this.setData({
      hiddenmodalput: !this.data.hiddenmodalput
    })
  },
  //取消按钮
  cancel: function () {
    this.setData({
      hiddenmodalput: true
    });
  },
  //确认
  confirm: function () {
    var phone = this.data.phone;
    this.setData({
      hiddenmodalput: true
    })

    wx.request({
      url: "https://www.enjoyyz.com:8443/yinzuo/updateUser?userId=" + app.globalData.userid + "&phoneNumber=" + phone ,
        method: 'POST',
        dataType: 'JSON',
        success: function (res) {
          console.log(phone,'phone')
          console.log(app.globalData.nickName, 'app.globalData.nickName')
      },
    })
  },



    // 选择的平台改变时的触发方法
    plantformChange: function(e) {
      var platformuser;
      var that = this;
      this.setData({
          plantformIndex: e.detail.value,
         
      })
      console.log(this.data.plantformIndex)
      if (this.data.plantformIndex == 0) {
        platformuser = '微信';
        // this.data.platform1= platformuser;
      } else if (this.data.plantformIndex == 1) {
        platformuser='QQ'
        // this.data.platform1 = platformuser;
      }
      console.log(platformuser,'platformuser')
      wx.request({
        // url: 'https://www.enjoyyz.com:8443/yinzuo/updateUser',
        url: "https://www.enjoyyz.com:8443/yinzuo/updateUser?userId=" + app.globalData.userid + "&platform=" + platformuser,
        method: 'POST',
        success: function (res) {
          console.log(res)
          console.log(app.globalData.platform, 'app.globalData.platform')
          app.globalData.platform= platformuser
          platform1: platformuser
          that.onLoad()
        },
        
      })
    },

    // // 切换角色的触发方法
    // roleChange: function(e) {
    //   this.setData({
    //       roleIndex: e.detail.value
    //   })
    //   console.log(this.data.roleIndex)
    // },



  // 切换角色的触发方法
  roleChange: function (e) {
    var roleuser;
    var that = this;
    this.setData({
      roleIndex: e.detail.value,
    })
    console.log(this.data.roleIndex)
    if (this.data.roleIndex == 0) {
      roleuser = '学员';
      // this.data.platform1= platformuser;
    } else if (this.data.roleIndex == 1) {
      roleuser = '导师'
      // this.data.platform1 = platformuser;
    }
    console.log(roleuser, 'roleuser')
    wx.request({
      // url: 'https://www.enjoyyz.com:8443/yinzuo/updateUser',
      url: "https://www.enjoyyz.com:8443/yinzuo/updateUser?userId=" + app.globalData.userid + "&userType=" + that.data.roleIndex,
      method: 'POST',
      success: function (res) {
        console.log(res);
        app.globalData.userType = roleuser
        userType:roleuser
        that.onLoad()
      },

    })
  },



    // 点击游戏昵称时的触发方法
    gamenameChange: function(e) {
      console.log(e)
      var currentStatu = e.currentTarget.dataset.statu;
      this.util(currentStatu)
    },

    // 获取输入的修改
    newNameInput: function(e) {
        this.gameName = e.detail.value;
    },

    // 弹窗动画的 局部 工具类
    util: function(currentStatu) {
        /* 动画部分 */
        // 第1步：创建动画实例
        var animation = wx.createAnimation({
            duration: 200, //动画时长  
            timingFunction: "linear", //线性  
            delay: 0 //0则不延迟  
        });

        // 第2步：这个动画实例赋给当前的动画实例  
        this.animation = animation;

        // 第3步：执行第一组动画  
        animation.opacity(0).rotateX(-100).step();

        // 第4步：导出动画对象赋给数据对象储存  
        this.setData({
            animationData: animation.export()
        })

        // 第5步：设置定时器到指定时候后，执行第二组动画  
        setTimeout(function() {
            // 执行第二组动画  
            animation.opacity(1).rotateX(0).step();
            // 给数据对象储存的第一组动画，更替为执行完第二组动画的动画对象  
            this.setData({
                animationData: animation
            })
            //确定修改  
            if (currentStatu == "close") {
                var that = this;
                var val = this.gameName;
                console.log(val,'val')
                this.setData({
                    gameName: val,
                    showCgnModalStatus: false
                });
                // 相应的在这里跟后端交互并且更新页面值
                wx.showToast({
                    title: '修改成功',
                    icon: 'succes',
                    duration: 1000,
                    mask: true
                })

              wx.request({
                url: "https://www.enjoyyz.com:8443/yinzuo/updateUser?userId="+app.globalData.userid+"&nickName="+val,
                method: 'POST',
                dataType: 'JSON',
    
                success: function (res) {
                  console.log(res)
                  console.log(app.globalData.nickName,'app.globalData.nickName')
                },
                fail: function (res) { },
                complete: function (res) { },
              })
            }
            // 取消修改
            if (currentStatu == "cancel") {
                this.setData({
                    showCgnModalStatus: false
                });
            }
        }.bind(this), 200)
        // 显示  
        if (currentStatu == "open") {
            this.setData({
                showCgnModalStatus: true
            });
        }
    },

  phoneInput: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },


})